// import {App} from "./app"

///<reference path='./app.ts'/>
class UILayer extends lg.Container
{
    private _progressBg:lg.Bitmap=null;
    private _progressBar:lg.Bitmap=null;
    private _btnmusic:lg.Bitmap=null;
    private _btnBack:lg.Bitmap=null;

    constructor(){
        super();
        this.initSound();
         this.addEventListener("added", this.addedHandler);
    }
    private addedHandler(e) {
        e.target.removeEventListener("added", e.target.addedHandler);
        e.target.init();
    }
    private init():void
    {
        this.y=20;
        let canvas:HTMLCanvasElement=<HTMLCanvasElement>this.stage.canvas;
        this._progressBg = new lg.Bitmap( App.loadManager.getImage("progress_bg") ); 
        this._progressBg.x=canvas.width-this._progressBg.getBounds().width>>1;
        this._progressBg.y=52;
        this.stage.addChild(this._progressBg);

        this._progressBar = new lg.Bitmap( App.loadManager.getImage("progress_bar") ); 
       this._progressBar.x= canvas.width-this._progressBar.getBounds().width>>1;
        // this._progressBar.x=(canvas.width-this._progressBar.getBounds().width>>1)-this._progressBar.getBounds().width*0.5;;
        this._progressBar.y=54;
        
        this.stage.addChild(this._progressBar);

        this._btnBack = new lg.Bitmap( App.loadManager.getImage("btnback") ); 
        this._btnBack.x=40;
        this._btnBack.y=40;   
        this._btnBack.addEventListener("click",()=>{window["backBtnHandler"]()},null);
        this.stage.addChild(this._btnBack);

        this._btnmusic = new lg.Bitmap( App.loadManager.getImage("btnmusic") );
        this._btnmusic.x=canvas.width-40-this._btnmusic.getBounds().width;
        this._btnmusic.y=40;
        this._btnmusic.addEventListener("click",(e)=>{this.musicHandler(e)},null)
        this.stage.addChild(this._btnmusic);
        
       var time:lg.Text=new lg.Text("0分", "40px Arial","#FF00FF");
       var totalTime:number=180;
       var t:number=totalTime;
       time.text=t+"秒";
       time.textAlign="center"
       time.x=canvas.width>>1
       time.y=10;
       this.stage.addChild(time);
      
       setInterval(()=>{
           this._progressBar.scaleX=t/totalTime;
           t--;
           time.text=t+"秒";
           if(t==0) 
           {
               t=totalTime;
           }
       },1000);
    }
    private static musicPlaying:boolean=true;
    private musicHandler(e):void
    {
        window["musicHandler"]()
        // if(UILayer.musicPlaying)
        // {
        //     UILayer.bgSound.stop();
        //     UILayer.eatSound.stop();
        //     UILayer.musicPlaying=false;
        // }else
        // {
        //     UILayer.bgSound.play();
        //     UILayer.musicPlaying=true;
        // }
    }
    public static eatSoundPlay()
    {
        window["eatSoundPlay"]()
        // if( UILayer.musicPlaying)
        //     UILayer.eatSound.play();
    }
    private initSound()
    {
        var sounds = [
            {src:"../res/assets/audios/bg.mp3", id:"bg"},
            {src:"../res/assets/audios/eat.mp3", id:"eat"}
        ];
        lg.Sound.alternateExtensions=["mp3"];
        lg.Sound.on("fileload", this.loadHandler, this);
        lg.Sound.registerSounds(sounds);
       // lg.Sound.registerSound("res/assets/audios/eat.mp3", "eat");
 
    }
    public static bgSound:lg.AbstractSoundInstance;
    public static eatSound:lg.AbstractSoundInstance;
    private loadHandler(event) {
     
    UILayer.bgSound = lg.Sound.play("bg"); 
    // instance.volume = 0.1;
    UILayer.bgSound.setLoop(-1);

    UILayer.eatSound= lg.Sound.createInstance("eat"); 
     //instance.volume = 0.1;
 }
}